﻿using StrokeCenterSystem.Bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace StrokeCenterSystem.Core
{
    public class LoginAttribute : ActionFilterAttribute
    {
        private bool isNeed = true;
        /// <summary>
        /// 是否需要验证
        /// </summary>
        public bool IsNeed
        {
            get { return isNeed; }
            set { isNeed = value; }
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            //获取winform传递过来的登录参数
            HttpRequestBase bases = (HttpRequestBase)filterContext.HttpContext.Request;
            //获取url中的参数
            string queryString = bases.QueryString.ToString();
            string[] parameters = queryString.Split('&');
            string isWinform = bases["isWinform"];

            if (!IsNeed)
            {
                return;
            }

            if (LoginManager.GetLoginUser() == null || isWinform == "1")
            {
                bool flag = false;
                LoginManager.Logout();
                if (parameters.Length == 9)
                {
                    ///winform
                    SysLoginUser loginUser = new SysLoginUser();
                    int i = 0;
                    foreach (var item in parameters)
                    {
                        string[] paraInfo = item.Split('=');
                        switch (paraInfo[0])
                        {
                            case "userid":
                                i++;
                                loginUser.Personnel_id = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "username":
                                i++;
                                loginUser.Name = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "currdepcode":
                                i++;
                                loginUser.CurrDepCode = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "currdepname":
                                i++;
                                loginUser.CurrDepName = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "jobtitle":
                                i++;
                                loginUser.JobTitle = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "positiontitle":
                                i++;
                                loginUser.PositionTitle = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "departmentcode":
                                i++;
                                loginUser.DepartCode = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "departmentname":
                                i++;
                                loginUser.DepartName = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8);
                                break;
                            case "isWinform":
                                i++;
                                loginUser.IsWinform = HttpUtility.UrlDecode(paraInfo[1], System.Text.Encoding.UTF8) == "1" ? 1 : 0;
                                break;
                            default:
                                break;
                        }
                    }
                    if (i == 9)
                    {
                        LoginManager.SetLoginCookie(loginUser);
                        flag = true;
                    }
                }

                if (!flag)
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Login", action = "Index", area = string.Empty }));
                }
            }
        }
    }

}